package com.hzit.hzb.core.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 类名：
 * 作者：WF
 * 功能：全局跨域【解决方案一】
 */
// @Configuration
// public class MyWebConfig implements WebMvcConfigurer {
//
// 	@Override
// 	public void addCorsMappings(CorsRegistry registry) {
// 		registry.addMapping("/**")
// 				.allowCredentials(true)
// 				.allowedHeaders("*")
// 				.allowedOrigins("*")
// 				.allowedMethods("*");
// 	}
// }

/**
 * 类名：
 * 作者：WF
 * 功能：全局跨域【解决方案二】
 */
@Configuration
public class MyWebConfig {
	@Bean
	public CorsFilter corsFilter(){
		UrlBasedCorsConfigurationSource configSource = new UrlBasedCorsConfigurationSource();
		CorsConfiguration config = new CorsConfiguration();
		config.setAllowCredentials(true);
		config.addAllowedHeader("*");
		config.addAllowedMethod("*");
		config.addAllowedOrigin("*");
		configSource.registerCorsConfiguration("/**",config);
		CorsFilter corsFilter = new CorsFilter(configSource);
		return corsFilter;
	}
}
